package com.cloudpivot.project.rabbitmq.service;

import com.rabbitmq.client.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class MayCurDelayProducerService {
    @Value("${rabbitmq-config.host-name}")
    private String HOST_NAME;
    @Value("${rabbitmq-config.host-port}")
    private Integer HOST_PORT;
    @Value("${rabbitmq-config.username}")
    private String USER_NAME;
    @Value("${rabbitmq-config.password}")
    private String PASSWORD;
    private static final String MAYCUR_QUEUE_NAME = "normal_queue";
    private static final String MAYCUR_EXCHANGE_NAME = "normal_exchange";
    private static final String MAYCUR_VIRTUAL_HOST = "/maycur_opdca";
    private static final String MAYCUR_ROUTINGKEY = "normal_1";


    public void sendDelayMaycurToRabbitMQ(String message)throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HOST_NAME);
        factory.setPort(HOST_PORT);
        factory.setUsername(USER_NAME);
        factory.setPassword(PASSWORD);
        factory.setHost(HOST_NAME);
        factory.setVirtualHost(MAYCUR_VIRTUAL_HOST);
        Connection rabbitMQConnection= factory.newConnection();
        Channel channel = rabbitMQConnection.createChannel();
        channel.exchangeDeclare(MAYCUR_EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, new HashMap<>());
        channel.basicPublish(MAYCUR_EXCHANGE_NAME,MAYCUR_ROUTINGKEY,MessageProperties.TEXT_PLAIN,message.getBytes("UTF-8"));
        channel.close();
        rabbitMQConnection.close();
    }


}
